import 'package:flutter/material.dart';
import 'package:shortmovieapp/common.dart';
import 'package:shortmovieapp/myController/RequestServerController.dart';
import 'package:shortmovieapp/myController/appController.dart';
import 'package:shortmovieapp/myPages/KeepAliveWrapper.dart';
import 'package:shortmovieapp/myPages/mePage.dart';
import 'package:shortmovieapp/data/viewData.dart';
import 'package:shortmovieapp/myPages/recommendedPage.dart';
import 'package:shortmovieapp/myPages/wanderPage.dart';
import 'package:shortmovieapp/myPages/favioritesPage.dart';


class myHomePage extends StatefulWidget {
  final MyPageTag defaultPage;

  const myHomePage({super.key, this.defaultPage = MyPageTag.recommended});

  @override
  State<myHomePage> createState() => myHomePageState();
}

class myHomePageState extends State<myHomePage> {
  late MyPageTag _currentPageType;

  @override
  void initState() {
    super.initState();
    _currentPageType = widget.defaultPage;

    if(widget.defaultPage != MyPageTag.recommended){
      requestRecommendedMovies();
    }

    }

void requestRecommendedMovies () async{
   var data = await RequestServerController().getRecommendedMoviesData(recommendedPage.s_recommendedTitles);
   appController().recommendedPageData = data;
}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: getPageWidget(_currentPageType),
        bottomNavigationBar: HomeBottomNavigationBar(defaultIndex:_currentPageType.index,onTapToShowPage:setToPage));
  }

  void setToPage(MyPageTag page) {
    if(_currentPageType == page)return;

    setState(() {
      _currentPageType = page;
    });
    
    Debug.log("set to page:$page");
  }

  Widget getPageWidget(MyPageTag pageTag) {

    //print("GetPageWidget:$pageTag");
    switch (pageTag) {
      case MyPageTag.recommended:
        return  KeepAliveWrapper(keepAlive: true,
                child: recommendedPage());
      case MyPageTag.favorites:
      var faviorites = appController().localData.faviorites;
        return  favioritesPage(movieItemsData:faviorites);
      case MyPageTag.me:
        return const mePage();
      case MyPageTag.wander:
      default:
        return const wanderPage();
    }
  }
}

class HomeBottomNavigationBar extends StatelessWidget {
  final int defaultIndex;
  final Function(MyPageTag)? onTapToShowPage;
  const HomeBottomNavigationBar({super.key,this.defaultIndex = 0,this.onTapToShowPage});

  @override
  Widget build(BuildContext context) {
    return  BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: defaultIndex,
      backgroundColor: Colors.black12,
      selectedItemColor: Colors.white,
      unselectedItemColor: Colors.white54,
      showUnselectedLabels: true,
      selectedLabelStyle: const TextStyle(fontSize: 14, color: Colors.white),
      unselectedLabelStyle:
          const TextStyle(fontSize: 12, color: Colors.white54),
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.movie), label: "逛逛"),
        BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
        BottomNavigationBarItem(icon: Icon(Icons.favorite), label: "收藏"),
        BottomNavigationBarItem(icon: Icon(Icons.people), label: "我的"),
      ],
      onTap: (value) {
        Debug.log("bottom bar on tap:$value ${MyPageTag.values.length}");

        if (value >= 0 && value < MyPageTag.values.length) {
          MyPageTag page = MyPageTag.values[value];

          onTapToShowPage?.call(page);
        }
      },
    );;
  }
}